﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.DAL;
using Domain.BLL;
using PagedList;
using ComputerShopWebsite.Models;

namespace ComputerShopWebsite.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /Products/
        //CSEntities cs = new CSEntities();
        public ActionResult Index(int? catid, int? page)
        {           

            ProductBLL _product = new ProductBLL();
            var result = _product.listAllProducts();

            int pageSize = 5;
            int pageNumber = (page ?? 1);
            int CatId = (catid ?? 0);

            ViewBag.Mota = "Nothing to show ...";

            if (!(CatId == 0 || catid == null))
            {
                result = result.Where(p => p.CategoryID == catid);
                ViewBag.Mota = "Bạn đang xem các  ..." + CatId;
            }
            else
            {
                ViewBag.Mota = "Đây là danh sách các sản phẩm của chúng tôi <br /> <br />";
            }
            return View(result.ToPagedList(pageNumber, pageSize));
            
        }

        [HttpPost]
        public ActionResult QuickSearch(string searchString, int? page)
        {
            ProductBLL _product = new ProductBLL();
            var result = _product.listAllProducts();

            ViewBag.searchString = searchString;            

            int pageSize = 5;
            int pageNumber = (page ?? 1);            

            ViewBag.Mota = "Nothing to show ...";
            
            if (!String.IsNullOrEmpty(searchString))
            {
                result = result.Where(p => p.ProductName.ToUpper().Contains(searchString.ToUpper())
                                        || p.Description.ToUpper().Contains(searchString.ToUpper()));
            }

            ViewBag.Mota = "Đây là danh sách các sản phẩm của chúng tôi <br /> <br />";
            return View(result.ToPagedList(pageNumber, pageSize));
        }


        public ActionResult AdvancedSearch()
        {
            
            return View();
        }
        
        public ActionResult AdvancedSearchResult(ComputerShopWebsite.Models.AdvancedSearch aS, int? page)
        {
            ProductBLL _product = new ProductBLL();
            var result = _product.listAllProducts();

            //ViewBag.searchString = searchString;            

            int pageSize = 5;
            int pageNumber = (page ?? 1);            

            ViewBag.Mota = "Nothing to show ...";
            
            if (!String.IsNullOrEmpty(aS.ProductName))
            {
                result = result.Where(p => p.ProductName.ToUpper().Contains(aS.ProductName.ToUpper())
                                        || p.Description.ToUpper().Contains(aS.ProductName.ToUpper()));
            }

            if ((aS.topPrice != 0) && (aS.floorPrice != 0) && (aS.topPrice > aS.floorPrice))
            {
                result = result.Where(p => p.SalePrice >= aS.floorPrice
                                            && p.SalePrice <=aS.topPrice);
            }

            ViewBag.Mota = "Đây là danh sách các sản phẩm của chúng tôi <br /> <br />";
            return View(result.ToPagedList(pageNumber, pageSize));

        }

        public ActionResult Details(int id = 0)
        {
            ProductBLL _p = new ProductBLL();
            _p.ProductID = id;

            _p.SingleRead();

            return View(_p);

        }
        public ActionResult Demo()
        {
            return View();
        }
        public ActionResult ProductByCategory(int CatId, int? page)
        {
            ProductBLL _product = new ProductBLL();
            int pageSize = 2;
            int pageNumber = (page ?? 1);
            return View(_product.ListProductByCategory(CatId).ToPagedList(pageNumber, pageSize));
        }

        public ActionResult PagingProducts(int? page)
        {
            ProductBLL _product = new ProductBLL();                       
            int pageSize = 2;
            int pageNumber = (page ?? 1);
            return View(_product.listAllProducts().ToPagedList(pageNumber,pageSize));
        }





    }
}
